
#include <stdio.h>
#include "user_uart.h"

#define TXD_PIN (GPIO_NUM_4)
#define RXD_PIN (GPIO_NUM_5)

#define UART_RECV_BIT BIT0
#define RX_BUF_SIZE 1024

static QueueHandle_t Uart_Rx_Queue = NULL; // 创建队列句柄
char rx_buffer[RX_BUF_SIZE + 1];

void uart_init(int baud_rate)
{

	Uart_Rx_Queue = xQueueCreate(3, sizeof(char[30]));

	// baud_rate需要进行检验，是否是正确的波特率，之后在写

	const uart_config_t uart_config = {
		.baud_rate = baud_rate,
		.data_bits = UART_DATA_8_BITS,
		.parity = UART_PARITY_DISABLE,
		.stop_bits = UART_STOP_BITS_1,
		.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
		.source_clk = UART_SCLK_DEFAULT,
	};
	// We won't use a buffer for sending data.

	// install uart driver
	uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0);

	// init uart config param
	uart_param_config(UART_NUM_1, &uart_config);

	vTaskDelay(100 / portTICK_PERIOD_MS);
	// set Tx Rx Rts Cts pin
	uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
	vTaskDelay(100 / portTICK_PERIOD_MS);
}

void uart_tx_SendData(const char *data)
{
	static const char *TX_TASK_TAG = "TX_TASK";
	esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO);

	const int len = strlen(data);
	const int txBytes = uart_write_bytes(UART_NUM_1, data, len);

	ESP_LOGI(TX_TASK_TAG, "Wrote %d bytes", txBytes);
}

void uart_rx_task(void *pt)
{
	static const char *RX_TASK_TAG = "RX_TASK";
	esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);

	xTaskCreate(uart_rx_info_analysis_task, "uart recv", 4096 * 2, NULL, 5, NULL);
	//  uint8_t *data = (uint8_t *)malloc(RX_BUF_SIZE + 1);
	while (1)
	{
		// uart read bytes 中的第4个参数ticks to wait代表最长阻塞时间
		const int rxBytes = uart_read_bytes(UART_NUM_1, rx_buffer, RX_BUF_SIZE, 1000 / portTICK_PERIOD_MS);
		if (rxBytes > 0)
		{
			rx_buffer[rxBytes] = 0;

			ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, rx_buffer);
			ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, rx_buffer, rxBytes, ESP_LOG_INFO);

			if (xQueueSend(Uart_Rx_Queue, rx_buffer, 100) != pdPASS)
				ESP_LOGI(RX_TASK_TAG, "queue is full");
			// uart_rx_info_analysis(rx_buffer);
		}
	}
	// free(data);
	vTaskDelete(NULL);
}

extern int listen_sock;
void uart_rx_info_analysis_task(void *pt)
{
	char str[50] = {0};
	char data[50];
	const char *rxTAG = "rx_info_analysis";

	while (1)
	{
		if (xQueueReceive(Uart_Rx_Queue, data, portMAX_DELAY) != pdPASS)
			continue;

		if (uart_strcmp("verity"))
		{
			uart_tx_SendData("hello");
		}
		else if (uart_strcmp("wifi stop"))
		{

			xEventGroupSetBits(s_wifi_event_group, WIFI_AP_STOP_BIT);
			uart_tx_SendData("123");
			// user_wifi_ap_stop();
		}
		else if (uart_strcmp("wifi start"))
		{
			// user_wifi_ap_restore();
			xEventGroupSetBits(s_wifi_event_group, WIFI_AP_SOFT_START_BIT);
			// user_wifi_ap_start("192.168.10.103");
			// xTaskCreate(tcp_server_init_task, "tcp_server", 4096 * 2, NULL, 5, NULL);
		}
		else if (uart_strcmp("wifi reboot"))
		{
			xEventGroupSetBits(s_wifi_event_group, WIFI_AP_REBOOT_BIT);
		}
		else if (sscanf(data, "wifi ip:\"%[^\"]", str) == 1)
		{
			ESP_LOGI(rxTAG, "%s", str);
			// 一个验证函数
			if (strlen(str) < 11 && strlen(str) > 15) // 验证长度
			{
				ESP_LOGI(rxTAG, "wifi ip len is invalied");
				return;
			}

			strcpy(my_wifi_ap_ip, str);
			xEventGroupSetBits(s_wifi_event_group, WIFI_AP_REBOOT_BIT);
		}
		else if (sscanf(data, "wifi ssid:\"%[^\"]", str) == 1)
		{
			strcpy(my_wifi_ssid, str);
			xEventGroupSetBits(s_wifi_event_group, WIFI_AP_REBOOT_BIT);
		}
		else if (sscanf(data, "wifi pass:\"%[^\"]", str) == 1)
		{
			if (strlen(str) < 8)
			{
				ESP_LOGI(rxTAG, "Password must be at least 8 digits");
				return;
			}
			strcpy(my_wifi_pass, str);
			xEventGroupSetBits(s_wifi_event_group, WIFI_AP_REBOOT_BIT);
		}
	}
	vTaskDelete(NULL);
}

int uart_strcmp(char *str)
{
	if (strcmp(rx_buffer, str) == 0)
		return 1;
	return 0;
}
